#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
THN TELEPORT version 2

Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

This library is dual-licensed.
  + For commercial software, please obtain a commercial license from THN Solutions LLC.

  + For free software, this library is licensed under GPL version 3. A summary of GPLv3 is
    listed below. You should also find a copy of GPLv3 as file License_GPLv3.txt included
	with the source-code files.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 3 as published by
    the Free Software Foundation  of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 06-Jan-10 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;

using Thn;
using Thn.Application.Utilities;
using Thn.Application.Configuration;
using Thn.Interface.Vcl;

using Thn.Communication.Teleporting;
using Thn.Communication.Teleporting.Logging;
using Thn.Communication.Teleporting.Settings;
using Thn.Communication.Teleporting.Connectors;
using Thn.Communication.Teleporting.Connectors.Remoting;
using Thn.Communication.Teleporting.Connectors.Loopback;

using Demo.Business;
#endregion

namespace Demo
{
    /// <summary>
    /// 
    /// </summary>
#if DEBUG 
    public
#else
    public
#endif 
    partial class fmServer : Form
    {
        #region Fields
        string mGateway = "ProductService";
        string mConfigFilename = "Teleport - Server Side.xml";
        ServerConnector mConnector = null;
        #endregion

        #region Properties
        #endregion

        #region Register Gateways
        bool mGatewaysRegistered = false;
        void RegisterGateways()
        {
            if (!mGatewaysRegistered)
            {
                Teleport.AddGateway(mGateway, new ProductService());

                //ensure that we only need to register once
                mGatewaysRegistered = true;
            }
        }
        #endregion

        #region UI Events processing

        #region Start
        private void btnStart_Click(object sender, EventArgs e)
        {
            if (lstConnectors.Text == string.Empty) MessageBox.Show("Please select a connector first", "Error", MessageBoxButtons.OK);
            else
            {
                //register gateways if necessary
                RegisterGateways();

                //build connector
                string sectionName = lstConnectors.Text;
                TeleportConfigurator configurator = new TeleportConfigurator();
                configurator.AutoInitialize = false;//I don't want to initialize immediately
                mConnector = configurator.Load(mConfigFilename, lstConnectors.Text) as ServerConnector;

                //subscribe to events
                mConnector.Started += new EventHandler(connector_Started);
                mConnector.Stopped += new EventHandler(mConnector_Stopped);

                //initialize connector
                mConnector.Start();
            }
        }        

        void connector_Started(object sender, EventArgs e)
        {
            Log.Information("Server is listening at {0}", mConnector.EffectiveUri);
        }
        #endregion

        #region Stop
        private void btnStop_Click(object sender, EventArgs e)
        {
            mConnector.Stop();
        }

        void mConnector_Stopped(object sender, EventArgs e)
        {
            Log.Information("Server stopped");
        }
        #endregion

        #endregion
        
        #region On Load
        /// <summary>
        /// Initialize this form when form is loaded.
        /// </summary>
        protected override void OnLoad(EventArgs e)
        {
            if (!DesignMode)
            {
                //extract connector names
                SimpleConfigurator configurator = new SimpleConfigurator();
                configurator.Load(mConfigFilename);
                for (int i = 0; i < configurator.Sections.Count; i++)
                {
                    ConfigSection section = configurator.Sections[i];
                    lstConnectors.Items.Add(section["Name"]);
                }
            }
            #region Defensive tracing
            else
            {
                //don't do anything at design-time
            }
            #endregion

            //let my ancestor process its own logic
            base.OnLoad(e);
        }
        #endregion
        
        #region Constructors
        /// <summary>
        /// Default constructor.
        /// </summary>
        public fmServer() 
        {
            InitializeComponent();
        }
        #endregion        
    }
}
